package com.qfedu.web.servlet;

import com.qfedu.pojo.Address;
import com.qfedu.pojo.Cart;
import com.qfedu.pojo.User;
import com.qfedu.service.AddressService;
import com.qfedu.service.impl.AddressServiceImpl;
import com.qfedu.web.common.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/4/6 9:39
 * 描述:
 */
@WebServlet("/addressservlet")
public class AddressServlet extends BaseServlet {

    AddressService addressService = new AddressServiceImpl();

    protected String defaultAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中，获取用户信息
        Object obj = req.getSession().getAttribute("user");
        if(obj == null){
            return "redirect:/login.jsp";
        }

        User user = (User) obj;
        int id = Integer.parseInt(req.getParameter("id"));

        addressService.setDefaultAddress(user.getId(), id);

        return "redirect:/addressservlet?method=getAddress";
    }

    protected String deleteAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            addressService.deleteById(id);
            //说法：重定向到 getAddress路径
            return "redirect:/addressservlet?method=getAddress";
        } catch (Exception e) {
            e.printStackTrace();

            return "redirect:/addressservlet?method=getAddress&state=0";
        }
    }

    protected String updateAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中，获取用户信息
        Object obj = req.getSession().getAttribute("user");
        if(obj == null){
            return "redirect:/login.jsp";
        }

        User user = (User) obj;

        String detail = req.getParameter("detail");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String id = req.getParameter("id");
        String level = req.getParameter("level");

        Address address = new Address();
        address.setId(Integer.parseInt(id));
        address.setName(name);
        address.setDetail(detail);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setLevel(Integer.parseInt(level));
        //新增收货地址
        addressService.update(address);


        //说法：重定向到 getAddress路径
        return "redirect:/addressservlet?method=getAddress";
    }

    protected String addAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中，获取用户信息
        Object obj = req.getSession().getAttribute("user");
        if(obj == null){
            return "redirect:/login.jsp";
        }

        User user = (User) obj;

        String detail = req.getParameter("detail");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");

        Address address = new Address();
        address.setName(name);
        address.setDetail(detail);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setLevel(0);
        //新增收货地址
        addressService.insert(address);


        //说法：重定向到 getAddress路径
        return "redirect:/addressservlet?method=getAddress";
    }

    protected String getAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取用户
        Object obj = req.getSession().getAttribute("user");
        if(obj == null){
            return "redirect:/login.jsp";
        }

        User user = (User) obj;

        //查询该用户地址列表
        List<Address> addressList  = addressService.selectAddresses(user.getId());
        //把数据存储到请求域
        req.setAttribute("addList", addressList);
        return "forward:/self_info.jsp";
    }
}
